<!DOCTYPE html >
<html>
<head>
    <base>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>原材料采购订单管理</title>
    <link rel="stylesheet" href="../../../res/layui-v2.5.6/css/layui.css" media="all">
    <style>
        body {
            margin: 10px;
        }

        .demo-carousel {
            height: 300px;
            line-height: 300px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="demoTable">
    搜索供应商：
    <div class="layui-inline">
        <input class="layui-input" name="id" id="supName"
               autocomplete="off" placeholder="请输入供应商名称">
    </div>

    时间：
    <div class="layui-input-inline">
        <input name="date" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input"
               type="text">
    </div>
    ——
    <div class="layui-input-inline">
        <input name="date" id="date1" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input"
               type="text">
    </div>
    订单状态：

    <div class="layui-inline layui-form" style="width:130px">
        <select name="shzt" id="shzt"
                autocomplete="off" class="layui-input">
            <option></option>
            <option value="10">未申请</option>
            <option value="20">已申请</option>
            <option value="60">审核通过</option>
            <option value="61">审核不通过</option>
            <option value="68">等待财务审核</option>
            <option value="70">财务审核通过</option>
            <option value="71">财务审核不通过</option>
            <option value="91">已入库</option>
        </select>
    </div>
    <button class="layui-btn" data-type="reload" id="sousuo">搜索</button>
    <button class="layui-btn" id="imgAdd" data-type="add">增加</button>
</div>
<table class="layui-hide" id="goods" lay-filter="demo"></table>

<script type="text/html" id="xx">
    <a class="layui-btn layui-btn-xs" lay-event="chakan" id="dindanxx">查看订单详情</a>
    <a class="layui-btn layui-btn-xs" lay-event="chakansh" id="shenhecx">查看审核详情</a>
</script>
<script type="text/html" id="barDemo3">
    {{# if(returnUser().companyId!=="0"){ }}
        {{# if(d.reviewState == 10 ){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="ApplyImmediately">立即申请</button>
        {{# }else if(d.reviewState == 60 ||d.reviewState == 71){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="financial">提交财务审核</button>
        {{# }else if(d.reviewState == 68){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="financial">
            提交财务审核
        </button>
        {{# }else if(d.reviewState == 70){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="joinTheInventory">质检入库</button>
        {{# }else if(d.reviewState == 91){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="joinTheInventory">质检入库
        </button>
        {{# }else{ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="ApplyImmediately">
            立即申请
        </button>
        {{# } }}
    {{# }else{ }}
        {{# if(d.reviewState == 10 || d.reviewState == 61){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="ApplyImmediately">立即申请</button>
        {{# }else if(d.reviewState == 60 ||d.reviewState == 71){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="financial">提交财务审核</button>
        {{# }else if(d.reviewState == 68){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="financial">
            提交财务审核
        </button>
        {{# }else if(d.reviewState == 70){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="joinTheInventory">质检入库</button>
        {{# }else if(d.reviewState == 91){ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="joinTheInventory">质检入库
        </button>
        {{# }else{ }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled" disabled="" lay-event="ApplyImmediately">
            立即申请
        </button>
        {{# } }}
    {{# } }}
</script>

<script src="../../../res/layui-v2.5.6/layui.js"></script>
<script src="../../../res/js/util.js"></script>
<script type="text/html" id="barDemo">
    {{panduan(d)}}
</script>
<script type="text/html" id="zt">
    {{zt(d)}}
</script>
<script>
    function zt(d) {
        if (d.reviewState == '10') {
            return '<p class="layui-btn layui-btn-danger layui-btn-xs">未申请</p>';
        } else if (d.reviewState == '20') {
            return '<p class="layui-btn layui-btn-warm layui-btn-xs">已申请</p>';
        } else if (d.reviewState == '60') {
            return '<p class="layui-btn layui-btn-warm layui-btn-xs">总公司总经理通过</p>';
        } else if (d.reviewState == '61') {
            return '<p class="layui-btn layui-btn-danger layui-btn-xs">总公司总经理不通过</p>';
        } else if (d.reviewState == '91') {
            return '<p class="layui-btn layui-btn-warm layui-btn-xs">已入库</p>';
        } else if (d.reviewState == '68') {
            return '<p class="layui-btn layui-btn-warm layui-btn-xs">等待财务审核</p>';
        } else if (d.reviewState == '70') {
            return '<p class="layui-btn layui-btn-warm layui-btn-xs">总公司财务审核通过</p>';
        } else if (d.reviewState == '71') {
            return '<p class="layui-btn layui-btn-warm layui-btn-xs">总公司财务审核不通过</p>';
        }
    }

    function panduan(d) {
        var html = "";
        if (returnUser().companyId!=="0") {
            html = '<button class="layui-btn layui-btn-xs layui-btn-disabled">修改</button><button class="layui-btn layui-btn-xs layui-btn-disabled">删除</button>';
        } else if (d.reviewState === "10" || d.reviewState === "61"){
            html = '<a class="layui-btn layui-btn-xs" lay-event="edit" id="disa">修改</a><a class="layui-btn layui-btn-danger layui-btn-xs" id="disa" lay-event="del">删除</a>';
        }else {
            html = '<button class="layui-btn layui-btn-xs layui-btn-disabled">修改</button><button class="layui-btn layui-btn-xs layui-btn-disabled">删除</button>';
        }
        return html;
    }

    var openIndex = null;
    var table = null;
    layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function () {
        table = layui.table;
        var laydate = layui.laydate //日期
            , laypage = layui.laypage //分页
            , layer = layui.layer //弹层
            //, table = layui.table //表格
            , upload = layui.upload //上传
            , element = layui.element; //元素操作


        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //监听Tab切换
        element.on('tab(demo)', function (data) {
            layer.msg('切换了：' + this.innerHTML);
            console.log(data);
        });

        //执行一个 table 实例
        table.render({
            elem: '#goods'
            , height: 474
            , url: getRootPath() + 'hqPurchaseorder/showAll' //数据接口
            , page: true //开启分页
            ,totalRow: true  //开启合计
            , method: 'post'
            , cols: [[ //表头
                //{type: 'checkbox', fixed: 'left'}
                //, {field: 'purchaseId', title: '采购订单Id', width: 150, fixed: 'left', align: 'center'}
                {field: 'supName', title: '供应商名称', width: 120, align: 'center', totalRowText: '合计'}
                , {field: 'purchasePerson', title: '采购人', width: 120, align: 'center'}
                , {field: 'purchaseTime', title: '采购时间', width: 160, align: 'center'}
                , {field: 'purchaseSumprice', title: '采购总金额', width: 120, align: 'center', totalRow: true}
                , {field: 'count', title: '重量(g)', width: 80, align: 'center', totalRow: true,templet:function (d) {
                            return Math.round(d.count);
                        }}
                , {field: 'reviewState', title: '采购状态', width: 180, align: 'center', templet: '#zt'}
                , {field: 'remark', title: '采购备注', width: 120, align: 'center'}
                , {field: 'opTime', title: '操作时间', width: 160, align: 'center'}
                , {fixed: 'right', title: '查询信息', width: 210, align: 'center', toolbar: '#xx'}
                , {fixed: 'right', title: '操作', width: 115, align: 'center', toolbar: '#barDemo'}
                , {fixed: 'right', title: '提交', width: 165, toolbar: '#barDemo3', align: "center"}
            ]]
            , id: 'testReload'
        });

        //监听工具条
        table.on('tool(demo)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                //删除
                var id = data.purchaseId;
                num = 0;
                layer.confirm('确认删除？', function (index) {
                    num++;
                    if (num ===1){
                        //使用jquery的ajax方式向后台提交数据
                        var url = getRootPath() + 'hqPurchaseorder/deletes';
                        var data = {purchaseId: id};
                        $.ajax({
                            asyn: true,
                            type: "delete",
                            url: url,
                            data: data,
                            success: function (res) {
                                playMessage(res.message)
                                if (res.status === 20) {
                                    layer.msg(res.message, {
                                        icon: 1,
                                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                    }, function () {
                                        layer.close(index);
                                        table.reload("testReload");//刷新表格
                                    });
                                } else {
                                    layer.msg(res.message, {
                                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                    }, function () {
                                        layer.close(index);
                                    });
                                }
                            },
                            error: function (fh) {
                                layer.msg(fh.message, {
                                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                }, function () {
                                    layer.close(index);
                                });
                            }
                        }, "json");
                    }
                });
            } else if (layEvent === 'edit') {
                //修改
                var id = data.purchaseId;
                var urls = "purchaseUpdate.html?purchaseId=" + id;
                openIndex = layer.open({
                    type: 2,
                    area: ['80%', '90%'],//弹出框大小
                    shift: 1,//弹出框动画效果
                    content: [urls, 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                });
            } else if (layEvent === 'chakan') {
                //查看明细
                var url = "purchasedetails.html?purchaseId=" + data.purchaseId;
                layer.open({
                    type: 2,
                    area: ['76%', '75%'],//弹出框大小
                    shift: 1,//弹出框动画效果
                    content: [url, 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                });
            } else if (layEvent === 'chakansh') {
                //查看审核
                var url = "reviewdetailed.html?purchaseId=" + data.purchaseId;
                layer.open({
                    type: 2,
                    area: ['76%', '75%'],//弹出框大小
                    shift: 1,//弹出框动画效果
                    content: [url, 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                });
            } else if (layEvent === 'ApplyImmediately') {
                //提交申请
                var id = data.purchaseId;
                console.info(id);
                var num=0;
                layer.confirm('确认申请？', function (index) {
                    num++;
                    if(num ===1){
                        $.ajax({
                            url: getRootPath() + 'hqPurchaseorder/updateisva',
                            data: {purchaseId: id},
                            dataType: 'json',
                            type: 'POST',
                            success: function (obj) {
                                if (obj.status === 20) {
                                    layer.msg(obj.message);
                                    playMessage(obj.message);
                                    table.reload('testReload');
                                } else {
                                    layer.msg(obj.message);
                                }
                            },
                            error: function () {

                            }
                        });
                    }
                });
            } else if (layEvent === 'joinTheInventory') {
                var id = data.purchaseId;
                console.info(id);
                var num=0;
                layer.confirm('确认加入库存？', function (index) {
                    num++;
                    if (num ===1){
                        $.ajax({
                            url: getRootPath() + 'hqPurchaseorder/joinTheInventory',
                            data: {purchaseId: id},
                            dataType: 'json',
                            type: 'POST',
                            success: function (obj) {
                                playMessage(obj.message);
                                if (obj.status === 20) {
                                    layer.msg(obj.message);
                                    table.reload('testReload');
                                } else {
                                    layer.msg(obj.message);
                                }
                            },
                            error: function (obj) {
                                layer.msg(obj.message);
                            }
                        });
                    }
                });
            } else if (layEvent === 'financial') {
                //财务申请
                var id = data.purchaseId;
                console.info(id);
                var num=0;
                layer.confirm('确认申请？', function (index) {
                    num++;
                    if(num ===1){
                        $.ajax({
                            url: getRootPath() + 'hqPurchaseorder/updateisvas',
                            data: {purchaseId: id},
                            dataType: 'json',
                            type: 'POST',
                            success: function (obj) {
                                if (obj.status === 20) {
                                    layer.msg(obj.message);
                                    playMessage(obj.message);
                                    table.reload('testReload');
                                } else {
                                    layer.msg(obj.message);
                                }
                            },
                            error: function () {

                            }
                        });
                    }
                });
            }
        });

        //条件查询
        var $ = layui.$;
        if (returnUser().companyId!=="0"){
            //console.info(returnUser());
            $("#imgAdd").attr({"disabled":"disabled"});
            $("#imgAdd").addClass("layui-btn-disabled");
        }
        $('#sousuo').on('click', function () {
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1
                    //重新从第 1页开始
                },
                where: {
                    supName: $("#supName").val(),
                    startTime: $("#date").val(),
                    overTime: $("#date1").val(),
                    reviewState: $("#shzt").val()
                }
            });
        });
        $('#imgAdd').on('click', function () {
            openIndex = layer.open({
                type: 2,
                area: ['80%', '90%'],//弹出框大小
                shift: 1,//弹出框动画效果
                content: ['purchaseorderAdd.html', 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });
        });

    });

    /**
     * 关闭层
     * @param msg 返回信息
     */
    function closeOpen(msg) {
        layer.close(openIndex);
        playMessage(msg)
        layer.msg(msg, {time: 1500})
        table.reload("testReload");//刷新表格
    }

    function closeOpens() {
        table.reload("testReload");//刷新表格
    }


</script>
</body>
</html>